﻿using NHibernate;
using NHibernate.Classic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IceZero.DataStore.Models
{
    /// <summary>
    /// 配置表
    /// </summary>
    public class DBConfig: ILifecycle,IValidatable
    {
        public DBConfig()
        {

        }

        /// <summary>
        /// 配置项id
        /// </summary>
        public virtual int Id { get; set; }

        /// <summary>
        /// 配置项键
        /// </summary>
        public virtual string Key { get; set; }

        /// <summary>
        /// 配置项值
        /// </summary>
        public virtual string Value { get; set; }

        /// <summary>
        /// 配置项备注
        /// </summary>
        public virtual string Remark { get; set; }

        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return true;
            }

            DBConfig cfg = obj as DBConfig;
            if (cfg == null)
            {
                return false;
            }

            if (Id != cfg.Id)
            {
                return false;
            }

            return true;
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode() + Key.GetHashCode();
        }

        public override string ToString()
        {
            return $"{Key},{Value}";
        }

        public virtual LifecycleVeto OnDelete(ISession s)
        {
            Console.WriteLine("life:delete");
            //不否决此行为
            return LifecycleVeto.NoVeto;
        }

        public virtual void OnLoad(ISession s, object id)
        {
            Console.WriteLine("life:load");
        }

        public virtual LifecycleVeto OnSave(ISession s)
        {
            Console.WriteLine("life:save");
            return LifecycleVeto.NoVeto;
        }

        public virtual LifecycleVeto OnUpdate(ISession s)
        {
            Console.WriteLine("life:update");
            return LifecycleVeto.NoVeto;
        }

        public virtual void Validate()
        {
            if (Key.Length > 100)
            {
                throw new ValidationFailure("the key name is too long");
            }
        }
    }
}
